﻿Imports System.Windows.Forms
Imports Microsoft.Win32

''' <summary>
''' Properties of the Application
''' </summary>
''' <remarks></remarks>
Public Class MyProperties

    Public Property MyPins As New Pins

    ''' <summary>
    ''' The working area of the user's screen(s).
    ''' </summary>
    ''' <value>The Width of all monitors and the height of the primary.</value>
    ''' <returns>Bounds; Size</returns>
    ''' <remarks></remarks>
    Public Shared ReadOnly Property ScreenBounds As Size
        Get
            Return New Size(Screen.PrimaryScreen.WorkingArea.Size.Width, Screen.PrimaryScreen.WorkingArea.Size.Height)
        End Get
    End Property

    ''' <summary>
    ''' The location of the user's wallpaper.
    ''' </summary>
    ''' <value>The location of the wallpaper.</value>
    ''' <returns>Wallapaper; String</returns>
    ''' <remarks></remarks>
    Public Shared Property Wallpaper As String
        Get
            Return Registry.CurrentUser.OpenSubKey("Control Panel\Desktop", False).GetValue("Wallpaper").ToString
        End Get
        Set(value As String)
            Registry.CurrentUser.OpenSubKey("Control Panel\Desktop", True).SetValue("Wallpaper", value)
        End Set
    End Property

    ''' <summary>
    ''' The main window of the Application.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared ReadOnly Property DockWindow As MainWindow
        Get
            Return DirectCast(My.Application.MainWindow, MainWindow)
        End Get
    End Property

    Public Shared ReadOnly Property isDesktopVisible
        Get
            Dim pName As String = GetProcessNameByHandle(GetForegroundWindow).tolower
            Return (pName = "progman") Or (pName = "explorer") Or (pName = "go gadget.vshost") Or (pName = "go gadget")
        End Get
    End Property

    Private Property PinStore As String
        Get
            Dim CurrentUser As RegistryKey = Registry.CurrentUser
            Dim zEntry As RegistryKey = CurrentUser.OpenSubKey("Software\Zytonic")
            If zEntry Is Nothing Then zEntry = CurrentUser.CreateSubKey("Software\Zytonic")
            Dim gEntry As RegistryKey = CurrentUser.OpenSubKey("Software\Zytonic\GoGadget")
            If gEntry Is Nothing Then zEntry = CurrentUser.CreateSubKey("Software\Zytonic\GoGadget")
            Return If(gEntry Is Nothing, Nothing, gEntry.GetValue("PinStore"))
        End Get
        Set(value As String)
            Dim CurrentUser As RegistryKey = Registry.CurrentUser
            Dim zEntry As RegistryKey = CurrentUser.OpenSubKey("Software\Zytonic", True)
            If zEntry Is Nothing Then zEntry = CurrentUser.CreateSubKey("Software\Zytonic")
            Dim gEntry As RegistryKey = CurrentUser.OpenSubKey("Software\Zytonic\GoGadget", True)
            If gEntry Is Nothing Then zEntry = CurrentUser.CreateSubKey("Software\Zytonic\GoGadget")
            gEntry.SetValue("PinStore", value)
        End Set
    End Property

    Public Sub AddPin(PinData As PinData)
        MyPins.Pins.Add(PinData)
        PinStore = MyPins.Serialize()
    End Sub

    Public Sub RemovePin(PinData As PinData)
        MyPins.Pins.Remove(PinData)
        PinStore = MyPins.Serialize()
    End Sub

    Public Sub New()
        Dim Store As String = PinStore
        If Store <> Nothing Then
            MyPins = New Pins(Store)
        End If
    End Sub

    Shared Function isCursorWithinRadius() As Boolean
        Dim Radius As Integer = MyProperties.DockWindow.ActualWidth
        Return (Forms.Cursor.Position.X <= Radius) _
            AndAlso (Forms.Cursor.Position.Y < MyProperties.ScreenBounds.Height)
    End Function

    Shared Function isCursorAtEdge() As Boolean
        Return (Forms.Cursor.Position.X <= 1) _
            AndAlso (Forms.Cursor.Position.Y < MyProperties.ScreenBounds.Height) _
            AndAlso (Forms.Cursor.Position.Y > 40)
    End Function

End Class

Public Class Pins
    Public Property Pins As New List(Of PinData)

    ''' <summary>
    ''' Serialize the class into a JSON string.
    ''' </summary>
    ''' <returns>JSON; string</returns>
    ''' <remarks></remarks>
    Public Function Serialize() As String
        Return Newtonsoft.Json.JsonConvert.SerializeObject(Pins)
    End Function

    ''' <summary>
    ''' Deserialize a JSON string into a list(Of PinData) object.
    ''' </summary>
    ''' <param name="JSON"></param>
    ''' <remarks></remarks>
    Public Function Deserialize(JSON As String) As List(Of PinData)
        Return Newtonsoft.Json.JsonConvert.DeserializeObject(Of List(Of PinData))(JSON)
    End Function

    Public Sub New(JSON As String)
        Pins = Deserialize(JSON)
    End Sub

    Public Sub New()

    End Sub

End Class